﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SpellEditor
{
    public partial class Form1 : Form
    {
        public int version = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void buttonCreateSpell_Click(object sender, EventArgs e)
        {
            if (comboBoxSpellEffect.SelectedIndex == -1 || comboBoxSpellProperty.SelectedIndex == -1 ||
                comboBoxSpellType.SelectedIndex == -1 || textCooldown.Text == "" || textEffectLength.Text == "" ||
                textImageFilePath.Text == "" || textPower.Text == "" || textRange.Text == "" ||
                textRecovery.Text == "" || textSpeed.Text == "" || textSpellName.Text == "" ||
                textTargetArea.Text == "" || comboBoxSpellMoveType.SelectedIndex == -1)
            {
                MessageBox.Show("Please fill out all fields");
                return;
            }

            SpellType type;
            SpellProperty property;
            SpellEffect effect;
            SpellMoveType move;

            switch(comboBoxSpellType.GetItemText(comboBoxSpellType.SelectedItem))
            {
                case "Green":
                    type = SpellType.Green;
                    break;
                case "Blue":
                    type = SpellType.Blue;
                    break;
                case "Red":
                    type = SpellType.Red;
                    break;
                default:
                    type = SpellType.Max;
                    break;
            }

            switch(comboBoxSpellProperty.GetItemText(comboBoxSpellProperty.SelectedItem))
            {
                case "Heatable":
                    property = SpellProperty.Heatable;
                    break;
                case "Chillable":
                    property = SpellProperty.Chillable;
                    break;
                case "Conducatable":
                    property = SpellProperty.Conductable;
                    break;
                case "Soakable":
                    property = SpellProperty.Soakable;
                    break;
                default:
                    property = SpellProperty.Max;
                    break;
            }

            switch(comboBoxSpellEffect.GetItemText(comboBoxSpellEffect.SelectedItem))
            {
                case "None":
                    effect = SpellEffect.None;
                    break;
                case "Frozen":
                    effect = SpellEffect.Frozen;
                    break;
                case "Burned":
                    effect = SpellEffect.Burned;
                    break;
                case "Brittle":
                    effect = SpellEffect.Brittle;
                    break;
                case "Stunned":
                    effect = SpellEffect.Stunned;
                    break;
                case "Poisoned":
                    effect = SpellEffect.Poisoned;
                    break;
                default:
                    effect = SpellEffect.Max;
                    break;
            }

            switch (comboBoxSpellMoveType.GetItemText(comboBoxSpellMoveType.SelectedItem))
            {
                case "Caster To Target":
                    move = SpellMoveType.CasterToTarget;
                    break;
                case "On Target":
                    move = SpellMoveType.OnTarget;
                    break;
                default:
                    move = SpellMoveType.Max;
                    break;
            }

            listBoxSpells.Items.Add(new CSpell(type, property, effect, textSpellName.Text, textImageFilePath.Text, 
                false, Convert.ToInt32(textPower.Text),  Convert.ToInt32(textSpeed.Text), Convert.ToInt32(textRange.Text), 
                Convert.ToInt32(textRecovery.Text), Convert.ToInt32(textTargetArea.Text),  
                Convert.ToInt32(textEffectLength.Text),  Convert.ToInt32(textCooldown.Text), move));

            textCooldown.Text = "";
            textEffectLength.Text = "";
            textSpeed.Text = "";
            textSpellName.Text = "";
            comboBoxSpellEffect.SelectedIndex = -1;            
            comboBoxSpellProperty.SelectedIndex = -1;
            comboBoxSpellType.SelectedIndex = -1;
            comboBoxSpellMoveType.SelectedIndex = -1;
            textPower.Text = "";
            textRecovery.Text = "";
            textTargetArea.Text = "";
            textRange.Text = "";
            textImageFilePath.Text = "";
        }

        private void buttonEditSpell_Click(object sender, EventArgs e)
        {
            if (listBoxSpells.SelectedIndex < 0 || listBoxSpells.SelectedIndex > listBoxSpells.Items.Count - 1)
                return;

            if (comboBoxSpellEffect.SelectedIndex == -1 || comboBoxSpellProperty.SelectedIndex == -1 ||
                comboBoxSpellType.SelectedIndex == -1 || textCooldown.Text == "" || textEffectLength.Text == "" ||
                textImageFilePath.Text == "" || textPower.Text == "" || textRange.Text == "" ||
                textRecovery.Text == "" || textSpeed.Text == "" || textSpellName.Text == "" ||
                textTargetArea.Text == "" || comboBoxSpellMoveType.SelectedIndex == -1)
            {
                MessageBox.Show("Please fill out all fields");
                return;
            }

            SpellType type;
            SpellProperty property;
            SpellEffect effect;
            SpellMoveType move;

            switch (comboBoxSpellType.GetItemText(comboBoxSpellType.SelectedItem))
            {
                case "Green":
                    type = SpellType.Green;
                    break;
                case "Blue":
                    type = SpellType.Blue;
                    break;
                case "Red":
                    type = SpellType.Red;
                    break;
                default:
                    type = SpellType.Max;
                    break;
            }

            switch (comboBoxSpellProperty.GetItemText(comboBoxSpellProperty.SelectedItem))
            {
                case "Heatable":
                    property = SpellProperty.Heatable;
                    break;
                case "Chillable":
                    property = SpellProperty.Chillable;
                    break;
                case "Conducatable":
                    property = SpellProperty.Conductable;
                    break;
                case "Soakable":
                    property = SpellProperty.Soakable;
                    break;
                default:
                    property = SpellProperty.Max;
                    break;
            }

            switch (comboBoxSpellEffect.GetItemText(comboBoxSpellEffect.SelectedItem))
            {
                case "None":
                    effect = SpellEffect.None;
                    break;
                case "Frozen":
                    effect = SpellEffect.Frozen;
                    break;
                case "Burned":
                    effect = SpellEffect.Burned;
                    break;
                case "Brittle":
                    effect = SpellEffect.Brittle;
                    break;
                case "Stunned":
                    effect = SpellEffect.Stunned;
                    break;
                case "Poisoned":
                    effect = SpellEffect.Poisoned;
                    break;
                default:
                    effect = SpellEffect.Max;
                    break;
            }

            switch (comboBoxSpellMoveType.GetItemText(comboBoxSpellMoveType.SelectedItem))
            {
                case "Caster To Target":
                    move = SpellMoveType.CasterToTarget;
                    break;
                case "On Target":
                    move = SpellMoveType.OnTarget;
                    break;
                default:
                    move = SpellMoveType.Max;
                    break;
            }

            listBoxSpells.Items[listBoxSpells.SelectedIndex] = new CSpell(type, property, effect, textSpellName.Text,
                textImageFilePath.Text, false, Convert.ToInt32(textPower.Text), Convert.ToInt32(textSpeed.Text), 
                Convert.ToInt32(textRange.Text), Convert.ToInt32(textRecovery.Text), Convert.ToInt32(textTargetArea.Text),
                Convert.ToInt32(textEffectLength.Text), Convert.ToInt32(textCooldown.Text), move);

            textCooldown.Text = "";
            textEffectLength.Text = "";
            textSpeed.Text = "";
            textSpellName.Text = "";
            textPower.Text = "";
            textRecovery.Text = "";
            textTargetArea.Text = "";
            textRange.Text = "";
            textImageFilePath.Text = "";
            comboBoxSpellEffect.SelectedIndex = -1;
            comboBoxSpellProperty.SelectedIndex = -1;
            comboBoxSpellType.SelectedIndex = -1;
            comboBoxSpellMoveType.SelectedIndex = -1;
        }

        private void buttonSaveList_Click(object sender, EventArgs e)
        {
            if (listBoxSpells.Items.Count <= 0)
            {
                MessageBox.Show("No spells to save");
                return;
            }

            string filename;
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.InitialDirectory = Application.StartupPath;
            sfd.Filter = "Binary Files|*.bin";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                filename = sfd.FileName;
                FileStream fs = File.Create(filename);
                BinaryWriter bw = new BinaryWriter(fs);

                // First writes the version of spell
                bw.Write(version);

                // Number of spells
                bw.Write(listBoxSpells.Items.Count);

                // Saves the spells
                foreach (CSpell spell in listBoxSpells.Items)
                {
                    spell.Save(bw);
                }
                bw.Close();
            }
        }

        private void buttonLoadList_Click(object sender, EventArgs e)
        {
            string filename;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Application.StartupPath;
            ofd.Filter = "Binary Files|*.bin";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                // Clear out all data
                textCooldown.Text = "";
                textEffectLength.Text = "";
                textSpeed.Text = "";
                textSpellName.Text = "";
                textPower.Text = "";
                textRecovery.Text = "";
                textTargetArea.Text = "";
                textRange.Text = "";
                textImageFilePath.Text = "";
                comboBoxSpellEffect.SelectedIndex = -1;
                comboBoxSpellProperty.SelectedIndex = -1;
                comboBoxSpellType.SelectedIndex = -1;
                comboBoxSpellMoveType.SelectedIndex = -1;
                listBoxSpells.Items.Clear();

                filename = ofd.FileName;
                FileStream fs = File.OpenRead(filename);
                BinaryReader br = new BinaryReader(fs);
                int _version = br.ReadInt32();
                int count = br.ReadInt32();
                for (int i = 0; i < count; ++i)
                {
                    CSpell spell = new CSpell();
                    spell.Load(br, _version);
                    listBoxSpells.Items.Add(spell);
                }
                br.Close();
            }
        }

        private void listBoxSpells_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxSpells.SelectedIndex == -1)
            {
                labelSelectedSpellType.Text = "Spell Type:";
                labelSelectedSpellProp.Text = "Spell Property:";
                labelSelectedMoveType.Text = "Spell Move Type:";
                labelSelectedSpellEffect.Text = "Spell Effect:";
                labelSelectedSpellName.Text = "Spell Name:";
                labelSelectedFilePath.Text = "Filepath:";
                labelSelectedPower.Text = "Power:";
                labelSelectedSpeed.Text = "Speed:";
                labelSelectedRange.Text = "Range:";
                labelSelectedRecovery.Text = "Recovery:";
                labelSelectedTargetArea.Text = "Target Area:";
                labelSelectedEffectLength.Text = "Effect Length:";
                labelSelectedCooldown.Text = "Cooldown:";
                return;
            }

            CSpell spell = (CSpell)listBoxSpells.SelectedItem;

            switch(spell.GetSpellType())
            {
                case SpellType.Green:
                    labelSelectedSpellType.Text = "Spell Type: Green";
                    break;
                case SpellType.Blue:
                    labelSelectedSpellType.Text = "Spell Type: Blue";
                    break;
                case SpellType.Red:
                    labelSelectedSpellType.Text = "Spell Type: Red";
                    break;
            }

            switch(spell.GetProperty())
            {
                case SpellProperty.Heatable:
                    labelSelectedSpellProp.Text = "Spell Property: Heatable";
                    break;
                case SpellProperty.Chillable:
                    labelSelectedSpellProp.Text = "Spell Property: Chillable";
                    break;
                case SpellProperty.Conductable:
                    labelSelectedSpellProp.Text = "Spell Property: Conductable";
                    break;
                case SpellProperty.Soakable:
                    labelSelectedSpellProp.Text = "Spell Property: Soakable";
                    break;
            }

            switch(spell.GetEffect())
            {
                case SpellEffect.Burned:
                    labelSelectedSpellEffect.Text = "Spell Effect: Burned";
                    break;
                case SpellEffect.Brittle:
                    labelSelectedSpellEffect.Text = "Spell Effect: Brittle";
                    break;
                case SpellEffect.Frozen:
                    labelSelectedSpellEffect.Text = "Spell Effect: Frozen";
                    break;
                case SpellEffect.None:
                    labelSelectedSpellEffect.Text = "Spell Effect: None";
                    break;
                case SpellEffect.Poisoned:
                    labelSelectedSpellEffect.Text = "Spell Effect: Poisoned";
                    break;
                case SpellEffect.Stunned:
                    labelSelectedSpellEffect.Text = "Spell Effect: Stunned";
                    break;
            }

            switch (spell.GetMoveType())
            {
                case SpellMoveType.CasterToTarget:
                    labelSelectedMoveType.Text = "Spell Move Type: Caster to Target";
                    break;
                case SpellMoveType.OnTarget:
                    labelSelectedMoveType.Text = "Spell Move Type: On Target";
                    break;
            }

            labelSelectedSpellName.Text = "Spell Name: " + spell.GetName();
            labelSelectedFilePath.Text = "Filepath: " + spell.GetFilePath();
            labelSelectedPower.Text = "Power: " + spell.GetPower().ToString();
            labelSelectedSpeed.Text = "Speed: " + spell.GetSpeed().ToString();
            labelSelectedRange.Text = "Range: " + spell.GetRange().ToString();
            labelSelectedRecovery.Text = "Recovery: " + spell.GetRecovery().ToString();
            labelSelectedTargetArea.Text = "Target Area: " + spell.GetTargetArea().ToString();
            labelSelectedEffectLength.Text = "Effect Length: " + spell.GetEffectLength().ToString();
            labelSelectedCooldown.Text = "Cooldown: " + spell.GetCooldown().ToString();
        }

        private void buttonDeleteSpell_Click(object sender, EventArgs e)
        {
            if (listBoxSpells.SelectedIndex == -1 && listBoxSpells.SelectedIndex < listBoxSpells.Items.Count)
                return;

            listBoxSpells.Items.RemoveAt(listBoxSpells.SelectedIndex);
            listBoxSpells.SelectedIndex = -1;
        }

        private void buttonClearList_Click(object sender, EventArgs e)
        {
            listBoxSpells.SelectedIndex = -1;
            listBoxSpells.Items.Clear();
        }
    }
}
